package com.mofang.powerdekorhelper.adapter;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewAdapter;
import com.mofang.powerdekorhelper.base.recycleview.BaseViewHolder;
import com.mofang.powerdekorhelper.model.OrderDetial;
import com.mofang.powerdekorhelper.utils.L;

import java.util.List;

/**
 * Created by luofei on 2017/8/26 0026.
 * 说明：
 */

public class ProductEditAdapter extends BaseRecycleViewAdapter {

    public ProductEditAdapter(List<?> data, Context mContext, int layoutId) {
        super(data, mContext, layoutId);
    }

    @Override
    protected <T> void convert(final BaseViewHolder holder, T bean, final int position) {

        final OrderDetial.ProductInfoList product = (OrderDetial.ProductInfoList) bean;

        holder.setText(R.id.edit_item_total_value, position + 1 + "");

        holder.setText(R.id.edit_item_type_edit, product.getProductModel());
        holder.setText(R.id.edit_item_price_edit, product.getPrice() + "");

        holder.setText(R.id.edit_item_value, product.getNum() + "");
        if (product.getNum() == null) {
            holder.setText(R.id.edit_item_value, "0");
        }

        holder.setText(R.id.edit_item_total_value, product.getTotalPrice() + "");

        if (position == 0) {
            holder.setImageResouse(R.id.edit_item_plus_imv, R.mipmap.plus_icon);
            holder.setOnclickListener(R.id.edit_item_plus_imv, new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    OrderDetial.ProductInfoList product = new OrderDetial().new ProductInfoList();
                    product.setProductModel(position + "");

                    mData.add(getItemCount(), new OrderDetial().new ProductInfoList());

                    notifyDataSetChanged();
                }
            });

        } else {
            holder.setImageResouse(R.id.edit_item_plus_imv, R.mipmap.reduce_icon);
            holder.setOnclickListener(R.id.edit_item_plus_imv, new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    mData.remove(position);

                    notifyDataSetChanged();
                }
            });
        }

        // plus
        holder.setOnclickListener(R.id.edit_item_plus, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TextView textView = (TextView) holder.itemView.findViewById(R.id.edit_item_value);
                int value = Integer.parseInt(textView.getText().toString());
                holder.setText(R.id.edit_item_value, ++value + "");

                //计算总价
                double price = Double.valueOf(holder.getText(R.id.edit_item_price_edit));
                double totalPrice = value * price;

                holder.setText(R.id.edit_item_total_value, String.valueOf(totalPrice));

                List<OrderDetial.ProductInfoList> productInfoLists = mData;

                productInfoLists.get(position).setNum(value);
                productInfoLists.get(position).setPrice(Double.parseDouble(holder.getText(R.id.edit_item_price_edit)));
                productInfoLists.get(position).setProductModel(holder.getText(R.id.edit_item_type_edit));
                productInfoLists.get(position).setTotalPrice(totalPrice);

                logInfo();

                notifyDataSetChanged();
            }
        });

        // reduce
        holder.setOnclickListener(R.id.edit_item_reduce, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TextView textView = (TextView) holder.itemView.findViewById(R.id.edit_item_value);

                int value = Integer.parseInt(textView.getText().toString());
                if (value > 0) {
                    holder.setText(R.id.edit_item_value, --value + "");

                    //计算总价
                    double price = Double.valueOf(holder.getText(R.id.edit_item_price_edit));
                    double totalPrice = value * price;

                    holder.setText(R.id.edit_item_total_value, String.valueOf(totalPrice));

                    List<OrderDetial.ProductInfoList> productInfoLists = mData;

                    productInfoLists.get(position).setNum(value);
                    productInfoLists.get(position).setPrice(Double.parseDouble(holder.getText(R.id.edit_item_price_edit)));
                    productInfoLists.get(position).setProductModel(holder.getText(R.id.edit_item_type_edit));
                    productInfoLists.get(position).setTotalPrice(totalPrice);

                    logInfo();

                    notifyDataSetChanged();
                }
            }
        });

        // 产品型号编辑框内容监听
        EditText typeTv = (EditText) holder.itemView.findViewById(R.id.edit_item_type_edit);
     /*   typeTv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {
                //计算总价
                double price = Double.valueOf(holder.getText(R.id.edit_item_price_edit));
                int value = Integer.parseInt(holder.getText(R.id.edit_item_value));
                double totalPrice = value * price;
                holder.setText(R.id.edit_item_total_value, totalPrice + "");

                List<OrderDetial.ProductInfoList> productInfoLists = mData;

                productInfoLists.get(position).setProductModel(holder.getText(R.id.edit_item_type_edit));

                logInfo();

                notifyDataSetChanged();
            }
        });*/

        // 产品单价编辑框内容监听
        EditText priceTv = (EditText) holder.itemView.findViewById(R.id.edit_item_price_edit);

       /* priceTv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                //计算总价
                double price = Double.valueOf(holder.getText(R.id.edit_item_price_edit));
                int value = Integer.parseInt(holder.getText(R.id.edit_item_value));
                double totalPrice = value * price;
                holder.setText(R.id.edit_item_total_value, totalPrice + "");

                List<OrderDetial.ProductInfoList> productInfoLists = mData;

                productInfoLists.get(position).setPrice(Double.parseDouble(holder.getText(R.id.edit_item_price_edit)));

                logInfo();

                notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });*/
    }

    private void logInfo() {

        List<OrderDetial.ProductInfoList> productInfoLists = mData;

        for (int i = 0; i < productInfoLists.size(); i++) {

            OrderDetial.ProductInfoList product = productInfoLists.get(i);

            if (i == 0) {
                L.i("ProductInfoList", "=============================================================================");
            }
            L.i("ProductInfoList", i + "------>" + product.getProductModel() + "---" + product.getPrice() + "---" + product.getNum() + "---" + product.getTotalPrice());
        }
    }

}
